package src;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.math.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CodeBlueGame extends Canvas implements Runnable {
	private BufferStrategy strategy;
	private long lastLoopTime;
	private boolean gameIsRunning;
	ArrayList<Warrior> playersWarriors;
	ArrayList<Warrior> aIsWarriors;
	
	public CodeBlueGame(){
		JFrame mainWindow = new JFrame();
		JPanel terrainPanel = (JPanel) mainWindow.getContentPane();
		terrainPanel.setPreferredSize(new Dimension(800, 800));
		terrainPanel.setLayout(null);
		setBounds(0,0,800,600);
		terrainPanel.add(this);
		setIgnoreRepaint(true);
		
		mainWindow.pack();
		mainWindow.setResizable(false);
		mainWindow.setVisible(true);
		
		createBufferStrategy(2);
		this.strategy = getBufferStrategy();
		
	}
	
	public void run(){
		
		this.gameIsRunning = true;
		
		while (gameIsRunning){
			long delta = System.currentTimeMillis() - lastLoopTime;
			lastLoopTime = System.currentTimeMillis();
			
			//Attack loop
			
			//Player attacks Enemy
			for(Warrior W: playersWarriors){
				for (Warrior E: aIsWarriors){
					if (W.attTimer <= 0){
						if (Math.sqrt(((W.getXPosition() - E.getXPosition()) * (W.getXPosition() - E.getXPosition())) + 
									((W.getYPosition() - E.getYPosition()) * (W.getYPosition() - E.getYPosition()))) > 
									W.getRange()){
							W.attack(E);
						}
					}
				}
			}
			
			//Enemy attacks Player
			
			for(Warrior W: aIsWarriors){
				for (Warrior E: playersWarriors){
					if (W.attTimer <= 0){
						if (Math.sqrt(((W.getXPosition() - E.getXPosition()) * (W.getXPosition() - E.getXPosition())) + 
									((W.getYPosition() - E.getYPosition()) * (W.getYPosition() - E.getYPosition()))) > 
									W.getRange()){
							W.attack(E);
						}
					}
				}
			}
			
			
			
		}
	}

}
